package org.geektimes.projects.microprofile.config.source;

import javax.servlet.ServletContext;
import java.util.Enumeration;
import java.util.Map;


/**
 * 基于 ServletContext 的外部化配置数据源
 *
 * @author biwin
 */
public class ServletContextConfigSource extends MapBasedConfigSource {

    private final ServletContext servletContext;

    public ServletContextConfigSource(ServletContext servletContext) {
        super(500, "ServletContext Init Parameters", true);
        this.servletContext = servletContext;
    }

    @Override
    protected void prepareConfigData(Map configData) {
        Enumeration<String> initParameterNames = servletContext.getInitParameterNames();
        while (initParameterNames.hasMoreElements()) {
            String paramName = initParameterNames.nextElement();
            configData.put(paramName, servletContext.getInitParameter(paramName));
        }
    }
}
